<?php
/*
 * Created on 12. mars. 2008
 *
 *
 */
 
 class tournaments_dao {
 
 static function addTournament($name, $rules, $info, $numberofplayers) {
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	$child = $xml->addChild('tournament');
 	$child->addChild('name', $name);
 	$child->addChild('rules', $rules);
 	$child->addChild('info', $info);
 	$child->addChild('status', 'initialized');
 	$child->addChild('users');
 	$child->addChild('numberofplayers', $numberofplayers);
 	
 	return $xml->asXML($file);
 }
 
 static function updateTournament($name, $rules, $info, $status, $numberofplayers){
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	//$child = $xml->addChild('tournament');
 	$children= $xml->children();
 	foreach($children as $child){
 		if ($child===$name){
 			$child->name = $name;
 			$child->rules = $rules;
 			$child->info = $info;
 			$child->status = $status;
 			$child->numberofplayers = $numberofplayers;
 			return $xml->asXML($file);
 		}
 	}
 	return false; 	
 }
 
 
 function readTournament($name) {
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	$tournaments = $xml->tournament;
 	foreach($tournaments as $tournament) {
			if((string)$tournament->name === $name) {
				return $tournament;
			}
		}
	return null;
 }
 
 function userRegistered($tournamentname, $name) {
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	$tournament = tournaments_dao::readTournament($tournamentname);
 	
 	foreach($tournament->users->user as $user) {
 		if((string)$user === $name) {
 			return true;
 		}
 	}
 	return false;
 }
 
 function listOfTournaments() {
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	return $xml->tournament;
 	}
 
 /**
  * @tournament: navn på turnering
  * @user      : navn på ny bruker.
  * @check : defaul = false. If true: returnerer true hvis brukeren finnes, ellers false. Legger ikke til
  */
 function addPlayer($tournament, $newuser, $check=false) {
 	$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	$tournaments = $xml->tournament;
 	foreach($tournaments as $child){
 		if ((string)$child->name === $tournament){ //rett turnering
 			$nr = $child->users->user;
 			$max= $child->numberofplayers;
 			if( count($nr) < $max) {
 				$users = $child->users->user;
 				$userArray = array();
 				foreach ($users as $user){
					if ((string)$user===$newuser){
 						if ($check){
 							return true;
 						} else { 
 							return "bruker finnes fra før<br/>";
 						}
					}
 				}
 				if ($check) return false;
 				$user = $child->users->addChild('user', $newuser);
 				$user->addAttribute('knockedout', 'false');
 				if ($xml->asXML($file)){
 					return "bruker lagt til";
 				}
 			} else if($check) {
 				return null;
 			} else {
 				return "beklager, maks antall spiller oppnådd";
 			}
 		}
 	}
 	return "det oppstod en feil";
 }
 
 //Denne funksjonen trenger bearbeidelse - fjerner ikke end-tag i xml-filen.
 
// function deleteTournament($name) {
// 	$file = "dao/tournaments.xml";
// 	$xml = simplexml_load_file($file);
// 	
// 	$tournaments = $xml->tournament;
// 	foreach($tournaments as $tournament) {
//			if((string)$tournament->name === $name) {
//				foreach($tournament->children() as $slett) {
//					$slett = null;
//				}
//				$tournament->name = null;
//				$tournament->numberofplayers = null;
//				$tournament->users = null;
//				$tournament->rules = null;
//				$tournament->info = null;
//				$tournament->status = null;
//				$tournament = null;
//			}
//		}
// 	echo $xml->asXML();
// }

	/* Virker ikke som den skal enda ....*/
	function addLossPlayer($username, $tournamentname) {
		$file = "dao/tournaments.xml";
 	$xml = simplexml_load_file($file);
 	$tournaments = $xml->tournament;
 	
	echo "<pre>"; 
 	
 	
 	foreach($tournaments as $child){
 		if ((string)$child->name === $tournamentname){ //rett turnering
 			foreach($child as $users)
 				foreach($users as $user) {
 					if($user == $username) {
						$user['knockedout']= "true";
						$xml->asXML($file);
						return true;
 					}
 				}
 			}
 		}
 	return false;
	}
	
	function hasLost($username, $tournamentname) {
		$user = null;
		$file = "dao/tournaments.xml";
 		$xml = simplexml_load_file($file);
 		$tournaments = $xml->tournament;
		//echo "<pre>";
		foreach($tournaments as $child) {			
 		if ((string)$child->name === $tournamentname){ //rett turnering
 			$obj = $child->users;
 			//print_r($obj);
 			//print_r($obj->user);
 			foreach($obj->user as $user){
 				//foreach($users as $user) {
 				//	print_r($user[1]);
 					if($user == $username) {
				//		print_r($user);
						if((string)$user['knockedout'] === "true") {
							return true;
 						}
 					}
 				//}
 			}
 		}
		}
		//exit();
		return false;
	}
	
	function getStatus($tournamentname) {
		$tournament = tournaments_dao::readTournament($tournamentname);
		return $tournament->status;
	}
 }
 
?>
